﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Commons.DataFilters;

namespace SD.DataAccess
{
    //[TrungL1]: Modified comment more clearly. Date: 14-Dec-2012
    /// <summary>
    /// TypeOfBusinessDao Class
    /// </summary>
    [Serializable]
    public class TypeOfBusiness
    {
        [NonSerialized]
        private readonly ServiceDBDataContext _context = new ServiceDBDataContext();

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="max">The max.</param>
        /// <returns></returns>
        public IQueryable<BusinessType> GetList(BusinessFilters filter, string sortType, int startRowIndex, int max)
        {
            IQueryable<BusinessType> query = from a in _context.BusinessTypes
                                             select a;

            query = GetListQuery(query, filter);
            query = GetListSort(query, sortType);

            // Limit the results
            query = query.Skip(startRowIndex).Take(max);

            return query;
        }

        /// <summary>
        /// Gets the list sort.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <returns></returns>
        private static IQueryable<BusinessType> GetListSort(IQueryable<BusinessType> query, string sortType)
        {
            bool sortDescending = false;
            if (!string.IsNullOrEmpty(sortType))
            {
                string[] values = sortType.Split(' ');
                sortType = values[0];

                if (values.Length > 1)
                {
                    sortDescending = (values[1] == "DESC");
                }

                switch (sortType)
                {
                    case "BusinessName":
                        query = sortDescending
                                    ? query.OrderByDescending(a => a.BusinessName)
                                    : query.OrderBy(a => a.BusinessName);
                        break;
                    case "SICCode":
                        query = sortDescending
                                    ? query.OrderByDescending(a => a.SICCode)
                                    : query.OrderBy(a => a.SICCode);
                        break;
                    default:
                        query = sortDescending
                                    ? query.OrderByDescending(a => a.BusinessId)
                                    : query.OrderBy(a => a.BusinessId);
                        break;
                }
            }

            return query;
        }

        /// <summary>
        /// Gets the list query.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        private static IQueryable<BusinessType> GetListQuery(IQueryable<BusinessType> data, BusinessFilters filter)
        {
            // Filter by beginning of the letters
            if (!string.IsNullOrEmpty(filter.BusinessName))
            {
                data = data.Where(a => a.BusinessName.Contains(filter.BusinessName));
            }

            // Filter by in-active & active Gor
            if (!string.IsNullOrEmpty(filter.SicCode))
            {
                data = data.Where(a => a.SICCode.Contains(filter.SicCode));
            }

            return data;
        }


        /// <summary>
        /// Gets the list count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public int GetListCount(BusinessFilters filter)
        {
            IQueryable<BusinessType> data = from a in _context.BusinessTypes
                                            select a;

            return GetListQuery(data, filter).Count();
        }

        /// <summary>
        /// Gets the gov office region by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>GovOfficeRegion object</returns>
        public BusinessType GetBusinessTypeById(int id)
        {
            BusinessType objResult = _context.BusinessTypes.FirstOrDefault(a => a.BusinessId == id);
            return objResult;
        }
    }
}
